package com.dianqu.weather.web.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.dianqu.weather.core.model.ClientManager;

public class ContextListener implements ServletContextListener,
		ServletRequestListener, HttpSessionListener {
	ApplicationContext ac;
	ClientManager clientManager;

	public void contextDestroyed(ServletContextEvent sce) {

	}

	public void contextInitialized(ServletContextEvent sce) {
		ac = WebApplicationContextUtils.getWebApplicationContext(sce
				.getServletContext());
		clientManager = ac.getBean("clientManager", ClientManager.class);
	}

	public void requestDestroyed(ServletRequestEvent sre) {

	}

	public void requestInitialized(ServletRequestEvent sre) {

	}

	public void sessionCreated(HttpSessionEvent se) {
		clientManager.addOnlineClient(se.getSession());
	}

	public void sessionDestroyed(HttpSessionEvent se) {
		clientManager.removeOnlineClient(se.getSession());
	}

}
